package club.ljwang.service;

import club.ljwang.dao.MemberMapper;
import club.ljwang.dao.OrderMapper;
import club.ljwang.dao.OrderSettingMapper;
import club.ljwang.entity.Result;
import club.ljwang.pojo.Member;
import club.ljwang.pojo.Order;
import club.ljwang.pojo.OrderInfo;
import club.ljwang.pojo.OrderSetting;
import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @Auther: wangl
 * @Date: 2020/3/4 00:54
 * @Description:
 */

@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderSettingMapper orderSettingMapper;

    @Autowired
    private MemberMapper memberMapper;

    @Autowired
    private OrderMapper orderMapper;


    @Override
    public Result submit(OrderInfo orderInfo) {

        OrderSetting orderSetting = orderSettingMapper.findOneByDate(orderInfo.getOrderDate());
        if (orderSetting == null) {
            return new Result(false, "医院今日休息!");
        }

        int number = orderSetting.getNumber();
        int reservations = orderSetting.getReservations();
        if (reservations >= number) {
            return new Result(false, "今日预约人数已满!");
        }

        String telephone = orderInfo.getTelephone();
        String name = orderInfo.getName();
        String idCard = orderInfo.getIdCard();
        Integer sex = orderInfo.getSex();

        Member member = memberMapper.findByTelephone(telephone);
        if (member == null) {
            member = new Member();
            member.setPhoneNumber(telephone);
            member.setName(name);
            member.setSex(String.valueOf(sex));
            member.setIdCard(idCard);
            member.setRegTime(new Date());
            memberMapper.add(member);
        }

        Integer setmealId = orderInfo.getSetmealId();
        Integer memberId = member.getId();
        Date orderDate = orderInfo.getOrderDate();
        String orderType = orderInfo.getOrderType();

        Order orderQueryParam = new Order();
        orderQueryParam.setSetmealId(setmealId);
        orderQueryParam.setMemberId(memberId);
        orderQueryParam.setOrderDate(orderDate);
        List<Order> orders = orderMapper.findByCondition(orderQueryParam);
        if (CollectionUtil.isNotEmpty(orders)) {
            return new Result(false, "已预约,请不要重复预约!");
        }
        Order order = new Order();

        order.setOrderDate(orderDate);
        if(orderInfo.getValidateCode()==null){
            order.setOrderType(Order.ORDERTYPE_TELEPHONE);
        }else{
            order.setOrderType(Order.ORDERTYPE_WEIXIN);
        }

        order.setMemberId(memberId);
        order.setSetmealId(setmealId);
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        orderMapper.add(order);




        //memberMapper.editReservationsByDate(orderDate);
        int i = memberMapper.editReservationsByDate2(orderDate,orderSetting.getVersion());
        if (i == 0) {
            throw new OptimisticLockingFailureException("更新OrderSetting失败!");
        }

        return new Result(true, "预约成功!",order);
    }

    @Override
    public Result findById(Integer id) {
        OrderInfo orderInfo = orderMapper.findById(id);
        return new Result(true,"查询成功",orderInfo);
    }

    @Override
    public void updatePayNo(String out_trade_no, Integer orderId) {
        orderMapper.updatePayNo(out_trade_no,orderId);
    }

    @Override
    public void updatePayStatus(String out_trade_no) {
        orderMapper.updatePayStatus(out_trade_no);
    }
}
